package com.myj.shiro.chapter6.service;
 
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.myj.shiro.chapter6.entity.User;

public class PasswordHelper {
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	
	private String algorithName = "md5";
	private final int hashInterations = 2;
	
	public void encryptPassword(User user){
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		
		String newPassword = new SimpleHash(
				algorithName,
				user.getPassword(),
				ByteSource.Util.bytes(user.getCredentialsSalt()),
				hashInterations).toHex();
		
		user.setPassword(newPassword);
	}
}
